<? 
		if(isset($_POST['btnsubmit']))
		{
			require("required/dbconnect.php"); 
			
			$description=$_POST['description'];
			$per_kg_rate=$_POST['per_kg_rate'];
			$minimum_rate=$_POST['minimum_rate'];
			$id=$_POST['id'];
			$country=$_POST['country'];
			$type_id=$_POST['type_id'];		
			$all_same_rate=$_POST['all_same_rate'];			
			$insert=mysql_query("update os_shipping_rate set description='$description',per_kg_rate='$per_kg_rate',minimum_rate='$minimum_rate',country_id='$country',all_same_rate='$all_same_rate' where id=$id")or die(mysql_error());
			
			mysql_query("delete from os_shipping_state where shipping_rate_id=$id");
			
			if($all_same_rate==0)
			{
				if(!empty($_POST['state'])){
					$state=$_POST['state'];
					foreach($state as $state_id)
					{
						
						mysql_query("insert into os_shipping_state(shipping_rate_id,state_id)values('$id','$state_id')")or die(mysql_error());
					}
				}
			}
			
			header("Location:shipping_view_full.php?action=5&id=$type_id");		
		}
		else
		{
				require("required/dbconnect.php"); 
				require("required/function.php"); 
				require("required/header.php"); 
				
				
				
				checkAccess("allow_shipping_management",1);
				if(empty($_GET["id"]))
				{
					errRedirect("2","Invalid Access",$_SERVER['PHP_SELF'],"9");
				}
				
				$id = $_GET["id"];
				
				$get_data=mysql_query("select * from os_shipping_rate where id=$id");
				
				if(mysql_num_rows($get_data)==0){
					errRedirect("2","Record not found",$_SERVER['PHP_SELF'],"");		
				}
				
				
				$a=mysql_fetch_array($get_data,MYSQL_ASSOC);
?>
	<script language="JavaScript" type="text/javascript" src="required/js/goAjax.js"></script>
				<script language="javascript">
					function changeState(countryId) 
					{
						if(countryId!="")
						{
							
							if(document.frm1.all_same_rate.value==1)
							{
								var dontg=1;
							}else{
								changeContent('../required/js/ajax_place.php?country_id='+countryId+'&tag=state[]&multiple=1','divState');
								
							}
								
						}
					}
					
					function changeVisibility(type)
					{
						
						if(type==1)
						{
							document.getElementById('divAllArea').style.visibility="visible";
							document.getElementById('divState').style.visibility="hidden";
						}else{
							var countryId=document.getElementById("country").value;
							document.getElementById('divAllArea').style.visibility="hidden";
							document.getElementById('divState').style.visibility="visible";
							changeContent('../required/js/ajax_place.php?country_id='+countryId+'&tag=state[]&multiple=1','divState');
						}
					
					}
					
					
					function validateFrm(frm1){
						if(document.frm1.description.value=="")
						{
							alert("Please enter description");
							document.frm1.description.focus();
							return false;
						}
						if(document.frm1.per_kg_rate.value=="")
						{
							alert("Please enter rate per kg");
							document.frm1.per_kg_rate.focus();
							return false;
						}
						
						if(document.frm1.minimum_rate.value=="")
						{
							alert("Please enter minimum rate");
							document.frm1.minimum_rate.focus();
							return false;
						}
						
						return true;
							
					}
				</script>

<table width="100%"  cellpadding="0" cellspacing="5" height="300">
  <td width="84%" align="left" valign="top">
  <form action="<?= $_SERVER['PHP_SELF'] ?>" name="frm1" method="post" onSubmit="return validateFrm(this)">
      <table width="100%">
        <td class="title" colspan="3"><strong>Edit Shipping Rate For
          <?= getDataById("os_shipping_type","type_name","id",$a['type_id']) ?>
          </strong></td>
        <tr>
          <td width="150">Description</td>
          <td>:</td>
          <td><input class="text" type="text" name="description" size="30" value="<?= $a['description'] ?>" maxlength="200"></td>
        </tr>
        <tr>
          <td>Rate Per Kg</td>
          <td>:</td>
          <td><?= $module_currency_code ?>
            <input class="text" type="text" name="per_kg_rate" size="5" value="<?= $a['per_kg_rate'] ?>" maxlength="5"></td>
        </tr>
        <tr>
          <td>Minimum Charge</td>
          <td>:</td>
          <td><?= $module_currency_code ?>
            <input class="text" type="text" name="minimum_rate" size="5" value="<?= $a['minimum_rate'] ?>" maxlength="5"></td>
        </tr>
        <script language="JavaScript" type="text/javascript" src="required/js/selectplace.js"></script>
        <tr valign="top">
          <td>Country</td>
          <td>:</td>
          <td width="1507">
		  <?
				$get_place=mysql_query("select * from os_country where status=1 order by main_country desc");
		?>
            <select name="country" id="country" class="text" onChange="changeState(this.value)">
         	<option value="">Please Select</option>
			<? 
				while($s=mysql_fetch_array($get_place,MYSQL_ASSOC))
				{
					if($s['id']==$a['country_id'])
					{
						$desslt="selected";
					}
					else
					{
						$desslt="";
					}				  
			?>
              <option value="<?= $s['id'] ?>" <?= $desslt ?>>
              <?= $s['engname'] ?>
              </option>
              <? 
										
				  }
			  ?>
            </select></td>
        </tr>
        <tr>
            <Td>Same rate for all area</Td><Td>:</Td>
            <td>
            <?
				if($a['all_same_rate']==1)
				{
					$slt1="selected";
					$slt0="";
					$divAll="";
					$divState=";visibility:hidden";
				}
				else
				{
					$slt1="";
					$slt0="selected";
					$divAll=";visibility:hidden";
					$divState="";
				}			
			?>
                <select name="all_same_rate" id="all_same_rate" onchange="changeVisibility(this.value)">
                 	<option value="0" <?= $slt0 ?>>No</option>
                    <option value="1" <?= $slt1 ?>>Yes</option>
                </select>
            </td>
        </tr>
        <tr valign="top">
          <td>State</td>
          <td>:</td>
          <td>
           <div id="divAllArea" style="position: absolute; float: left<?= $divAll ?>">The shipping rate for all areas under the selected country will be the same </div>
			<div id="divState" style="float: left<?= $divState ?>">
              <?
									
				  $state_arr=array();
				  
				  $get_shipping_state=mysql_query("select * from os_shipping_state where shipping_rate_id=".$id);
				  if(mysql_num_rows($get_shipping_state)>0)
				  {
					  while($ss=mysql_fetch_array($get_shipping_state,MYSQL_ASSOC))
					  {
						  if(!empty($ss['state_id']))
						  {
							  $state_arr[]=$ss['state_id'];
						  }
						  
					  }
				  }
					
			  
			  ?>
              <select name="state[]" class="text" multiple="multiple" size="10">
                <? 
									
									
					$get_place=mysql_query("select * from os_state where country_code=".$a['country_id']." order by order_id asc");
					$sslt="";
					while($s=mysql_fetch_array($get_place,MYSQL_ASSOC))
					{
															
						if(in_array($s['id'],$state_arr))
						{
							$sslt="selected";
							
						}else{
							$sslt="";
							
						}
						
				?>
                <option value="<?= $s['id'] ?>" <?= $sslt ?> >
                <?= $s['name_eng'] ?>
                </option>
                <? 
										
									}
								?>
              </select>
            </div></td>
        </tr>
        <tr>
          <td colspan="3"><input type="hidden" value="<?= $a['id'] ?>" name="id" />
          <input type="hidden" value="<?= $a['type_id'] ?>" name="type_id" />
            <input type="submit" value="Submit" name="btnsubmit">
            <input type="button" value="Cancel" onclick="window.open('shipping_view_full.php?id=<?= $a['type_id'] ?>','_self')"></td>
        </tr>
      </table>
    </form></td>
</table>
<? }require("required/footer.php"); ?>